<template>
  <div>
    <!-- 公共导入组件 -->=={{ type }}
    <upload-excel :on-success="success" />
  </div>
</template>

<script>
import { importEmployeeApi } from "@/api/employees";
import dayjs from "dayjs";
export default {
  name: "",
  data() {
    return {
      type: this.$route.query.type
    };
  },
  methods: {
    async success({ header, results }) {
      // debugger;
      // console.log(data, 3);
      // 如果是导入员工
      const userRelations = {
        入职日期: "timeOfEntry",
        手机号: "mobile",
        姓名: "username",
        转正日期: "correctionTime",
        工号: "workNumber"
      };
      //
      // var arr = [];
      // results.forEach(item => {
      //   var obj = {};
      //   Object.keys(userRelations).forEach(key => {
      //     //arr.push(item[item2])
      //     obj[userRelations[key]] = item[key];
      //     //userRelations[item2]
      //   });
      //   arr.push(obj);
      // });
      // console.log(arr, 8888);

      //var arr = [];
      //[{入职日期: 43535,姓名: "高大山",工号: 20089,手机号: 13041139879,转正日期: 43719}]
      var arr = results.map(item => {
        var obj = {};
        Object.keys(item).forEach(key => {
          // obj[userRelations[key]]=item[key]
          if (
            userRelations[key] == "timeOfEntry" ||
            userRelations[key] == "correctionTime"
          ) {
            obj[userRelations[key]] = new Date(this.formatDate(item[key], "/"));
            //return;
          } else {
            obj[userRelations[key]] = item[key];
          }
        });
        return obj;
      });

      const res = await importEmployeeApi(arr);
      // if (!res.success) {
      //   this.$message.success("导入手机号不能重复");
      // }
      console.log(res, 222);
      this.$router.back(); // 回到上一页
    },
    formatDate(numb, format) {
      const time = new Date((numb - 1) * 24 * 3600000 + 1);
      time.setYear(time.getFullYear() - 70);
      const year = time.getFullYear() + "";
      const month = time.getMonth() + 1 + "";
      const date = time.getDate() - 1 + "";
      if (format && format.length === 1) {
        return year + format + month + format + date;
      }
      return (
        year +
        (month < 10 ? "0" + month : month) +
        (date < 10 ? "0" + date : date)
      );
    },

    
  }
};
</script>
<style lang="less" scoped></style>
